﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MFG.Web.Controllers
{
    public class PageController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Cases()
        {
            return View();
        }
        public ActionResult CaseDetail(string id)
        {
            if (!CaseInfo.Cache.ContainsKey(id)) return HttpNotFound();
            ViewBag.caseInfo = CaseInfo.Cache[id];
            return View();
        }




    }
    public class CaseInfo
    {
        public string Title { get; set; }

        public string Id { get; set; }

        public string Thumb { get; set; }

        public List<string> Images { get; set; }

        static CaseInfo()
        {
            var text = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./Assets/resources/cases.json"));
            var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CaseInfo>>(text);
            cache = new Dictionary<string, CaseInfo>(list.Count);
            for (int i = list.Count-1; i >= 0; i--)
            {
                var item = list[i];
                item.Id = (i ).ToString();
                cache.Add(item.Id, item);
            }
        }
        private static Dictionary<string, CaseInfo> cache;
        public static Dictionary<string, CaseInfo> Cache => cache;
    }
}